package org.dromara.curd.dnnyun.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.curd.dnnyun.domain.bo.DnnyunConsumeOrderBo;
import org.dromara.curd.dnnyun.domain.vo.DnnyunConsumeOrderVo;
import org.dromara.curd.dnnyun.domain.DnnyunConsumeOrder;
import org.dromara.curd.dnnyun.mapper.DnnyunConsumeOrderMapper;
import org.dromara.curd.dnnyun.service.IDnnyunConsumeOrderService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 东云消费订单Service业务层处理
 *
 * @author wdm
 * @date 2024-02-29
 */
@RequiredArgsConstructor
@Service
public class DnnyunConsumeOrderServiceImpl implements IDnnyunConsumeOrderService {

    private final DnnyunConsumeOrderMapper baseMapper;

    /**
     * 查询东云消费订单
     */
    @Override
    public DnnyunConsumeOrderVo queryById(Long orderId){
        return baseMapper.selectVoById(orderId);
    }

    /**
     * 查询东云消费订单列表
     */
    @Override
    public TableDataInfo<DnnyunConsumeOrderVo> queryPageList(DnnyunConsumeOrderBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DnnyunConsumeOrder> lqw = buildQueryWrapper(bo);
        Page<DnnyunConsumeOrderVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询东云消费订单列表
     */
    @Override
    public List<DnnyunConsumeOrderVo> queryList(DnnyunConsumeOrderBo bo) {
        LambdaQueryWrapper<DnnyunConsumeOrder> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DnnyunConsumeOrder> buildQueryWrapper(DnnyunConsumeOrderBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DnnyunConsumeOrder> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getStoreId() != null, DnnyunConsumeOrder::getStoreId, bo.getStoreId());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderCode()), DnnyunConsumeOrder::getOrderCode, bo.getOrderCode());
        lqw.eq(bo.getFettle() != null, DnnyunConsumeOrder::getFettle, bo.getFettle());
        lqw.eq(bo.getCheckFettle() != null, DnnyunConsumeOrder::getCheckFettle, bo.getCheckFettle());
        lqw.eq(bo.getOrderChannel() != null, DnnyunConsumeOrder::getOrderChannel, bo.getOrderChannel());
        lqw.eq(StringUtils.isNotBlank(bo.getCardNo()), DnnyunConsumeOrder::getCardNo, bo.getCardNo());
        lqw.like(StringUtils.isNotBlank(bo.getMemberName()), DnnyunConsumeOrder::getMemberName, bo.getMemberName());
        lqw.eq(StringUtils.isNotBlank(bo.getFirstMainSaleUserJobNumber()), DnnyunConsumeOrder::getFirstMainSaleUserJobNumber, bo.getFirstMainSaleUserJobNumber());
        lqw.like(StringUtils.isNotBlank(bo.getCreateUserName()), DnnyunConsumeOrder::getCreateUserName, bo.getCreateUserName());
        lqw.between(params.get("beginOrderTime") != null && params.get("endOrderTime") != null,
            DnnyunConsumeOrder::getOrderTime ,params.get("beginOrderTime"), params.get("endOrderTime"));
        return lqw;
    }

    /**
     * 新增东云消费订单
     */
    @Override
    public Boolean insertByBo(DnnyunConsumeOrderBo bo) {
        DnnyunConsumeOrder add = MapstructUtils.convert(bo, DnnyunConsumeOrder.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setOrderId(add.getOrderId());
        }
        return flag;
    }

    /**
     * 修改东云消费订单
     */
    @Override
    public Boolean updateByBo(DnnyunConsumeOrderBo bo) {
        DnnyunConsumeOrder update = MapstructUtils.convert(bo, DnnyunConsumeOrder.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DnnyunConsumeOrder entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除东云消费订单
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean deleteByStoreId(DnnyunConsumeOrderBo dnnyunConsumeOrderBo) {
        LambdaQueryWrapper<DnnyunConsumeOrder> lqw = buildQueryWrapper(dnnyunConsumeOrderBo);
        return baseMapper.delete(lqw) > 0;
    }

    @Override
    public Boolean insertBatchByBo(List<DnnyunConsumeOrderBo> boList) {
        if(CollectionUtil.isNotEmpty(boList)){
            List<DnnyunConsumeOrder> batchInsertList = new ArrayList<>();
            for (DnnyunConsumeOrderBo bo:boList) {
                DnnyunConsumeOrder add = MapstructUtils.convert(bo, DnnyunConsumeOrder.class);
                validEntityBeforeSave(add);
                batchInsertList.add(add);
            }
            return baseMapper.insertBatch(batchInsertList);
        }
        return true;
    }
}
